package sn.gandal.agriculture.agrimobile.j2me.gui;

import java.io.IOException;
import java.util.Vector;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;
import sn.gandal.agriculture.agrimobile.j2me.businessObjects.Membre;
import sn.gandal.agriculture.agrimobile.j2me.businessObjects.BusinessServicesImpl;
import sn.gandal.agriculture.agrimobile.j2me.businessObjects.IBusinessServices;
import sn.gandal.agriculture.agrimobile.j2me.utilities.Configuration;

/**
 *
 * @author dysow
 */
public class RechercheAbonneList extends List implements CommandListener
{

    private Display display;
    private Displayable previousScreen;
    private Command cmdCancel;
    Command itemCommand;
    private Vector vecAbonneList = null;
    private String previousItemName;
    private String principalMenu;
    private RechercheAbonneList rechercheAbonneList = null;
    
    public RechercheAbonneList(Display display, Displayable previous,String principalMenu, String previousItemName)
    {
	super(Configuration.RECHERCHE_ABONNE_LIST, List.IMPLICIT);

	this.display = display;
	this.previousScreen = previous;
	this.previousItemName = previousItemName;
        this.principalMenu=principalMenu;

	this.initialiseComponents();
	this.initialiseCommands();
	this.addCommands();
	this.setCommandListener(this);
    }

    public RechercheAbonneList(Display display, Displayable previous)
    {
	this(display, previous,null, null);
    }

    private void initialiseComponents()
    {
	this.vecAbonneList = new Vector();
    }

    private void initialiseCommands()
    {
	this.cmdCancel = new Command("Cancel", Command.CANCEL, 1);

	this.itemCommand = new Command(previousItemName, Command.OK, 2);

    }

//    public void setPreviousItemName(String previousItemName)
//    {
//	this.previousItemName = previousItemName;
//    }
    private void addCommands()
    {

//	if (previousItemName != null)
//	{
//	    if (previousItemName.equals(Configuration.MENU_PRINCIPAL))
//	    {
//		this.addCommand(choisir);
//		this.addCommand(this.cmdCancel);
//	    }
//	    else if (previousItemName.equals(Configuration.MENU_GESTION_PARCELLES))
//	    {
//		this.addCommand(this.cmdCancel);
//		this.addCommand(this.cmdModifierAbonne);
//		this.addCommand(this.cmdSupprimerAbonne);
//	    }
//	}
//	else
//	{
//	    this.addCommand(this.cmdCancel);
//	    this.addCommand(this.cmdModifierAbonne);
//	    this.addCommand(this.cmdSupprimerAbonne);
//	}

	this.addCommand(this.cmdCancel);
	this.addCommand(this.itemCommand);

    }

    public void commandAction(Command c, Displayable d)
    {

	System.out.println("Le label du bouton cliqué :" + c.getLabel());

	if (c.getLabel().equals(Configuration.CMD_MODIFIER))
	{
	    this.modifierAction();
	}
	else if (c.getLabel().equals(Configuration.CMD_SUPPRIMER))
	{
	    this.supprimerAction();
	}
	else if (c.equals(this.cmdCancel))
	{
	    this.cancelAction();
	}else{
            System.out.println("Command not yet implemented !!! ");
        }
	

	System.out.println("Le label du bouton cliqué :" + c.getLabel());
    }

    private void cancelAction()
    {
	this.display.setCurrent(new RechercheAbonneForm(display, previousItemName, principalMenu));
    }

    private void modifierAction()
    {
	//Verifier si un abonné est choisi
	Membre selectedAbonne = this.getSelectedAbonne();
	if (selectedAbonne != null)
	{
	//    this.getEditAbonneForm().initialiseAbonne(selectedAbonne);
	    //this.display.setCurrent(this.getEditAbonneForm());
	//    this.getEditAbonneForm().getEditJoursDePassageForm(selectedAbonne).initialiseJourDePassage(selectedAbonne);
	   // this.display.setCurrent(ConfirmationForm.ConfirmAction("Etes-vous sûr de vouloir modifier cet abonné !!"), this.
		//    getEditAbonneForm());
	 //    this.display.setCurrent(this.getEditAbonneForm());
	}
	else
	{
	    String error = "Veuillez choisir un abonné pour le modifier";
	    this.display.setCurrent(new ErrorsForm(error), this);
	}
    }

    public void populateAbonneList(Membre[] abonneList)
    {
	this.deleteAll();//remove all existing items
	this.vecAbonneList.removeAllElements();

	Membre abonne;
	String text = "";

	//ADD
//	IBusinessServices businessServices = BusinessServicesImpl.getInstance();
//	Membre[] abonneListActif = businessServices.getListAbonnesActif();
//	abonneList = abonneListActif;
	//ADD

	for (int i = 0;i < abonneList.length;i++)
	{
	    abonne = abonneList[i];
	    this.vecAbonneList.addElement(abonne);

	    text = abonne.getDisplay();

	    this.append(" " + text + "", null);
	}
    }

   
    private Membre getSelectedAbonne()
    {
	int selectedIndex = this.getSelectedIndex();

	if (selectedIndex == -1)
	{
	    return null;
	}
	else
	{
	    return (Membre)this.vecAbonneList.elementAt(selectedIndex);
	}
    }

    private void supprimerAction()
    {
	//Verifier si un abonné est choisi
	Membre selectedAbonne = this.getSelectedAbonne();
	if (selectedAbonne != null)
	{

	//    this.getDeleteAbonneForm().initialiseAbonne(selectedAbonne);
	    //this.display.setCurrent(ConfirmationForm.ConfirmAction("Etes-vous sûr de vouloir modifier cet abonné !!"), this.
		    //getDeleteAbonneForm());
	//     this.display.setCurrent(this.getDeleteAbonneForm());
	}
	else
	{
	    String error = "Veuillez choisir un abonné pour l'éliminer";
	    this.display.setCurrent(new ErrorsForm(error), this);
	}
    }

   

    private int getAbonneIndex(int abonneId)
    {
	Membre abonne;

	for (int i = 0;i < this.vecAbonneList.size();i++)
	{
	    abonne = (Membre)this.vecAbonneList.elementAt(i);

	    if (abonne.getIdAbonne() == abonneId)
	    {
		return i;
	    }
	}

	return -1;
    }

    public void abonneEdited(int idAbonneInt)
    {
	int abonneIndex = this.getAbonneIndex(idAbonneInt);

	if (abonneIndex != -1)
	{

	    Membre abonne = new Membre();
	    IBusinessServices iBusinessServices = BusinessServicesImpl.getInstance();
	    abonne = iBusinessServices.getAbonneDetails(idAbonneInt);

	    this.vecAbonneList.setElementAt(abonne, abonneIndex);
	    this.set(abonneIndex, abonne.getDisplay(), null);

	}
    }

    public void abonneDeleted(int idAbonneInt)
    {
	int abonneIndex = this.getAbonneIndex(idAbonneInt);

	if (abonneIndex != -1)
	{
	    Membre abonne = new Membre();
	    IBusinessServices iBusinessServices = BusinessServicesImpl.getInstance();
	    abonne = iBusinessServices.getAbonneDetails(idAbonneInt);

	    this.vecAbonneList.removeElementAt(abonneIndex);
	    this.delete(abonneIndex);

	}
    }

//    public Membre abonneChoisi(int idAbonneInt)
//    {
//	int abonneIndex = this.getAbonneIndex(idAbonneInt);
//        Membre abonne =null;
//	if (abonneIndex != -1)
//	{
//	    IBusinessServices iBusinessServices = BusinessServicesImpl.getInstance();
//	    abonne = iBusinessServices.getAbonneDetails(idAbonneInt);
//	    /// Rediriger directement vers le menu de gestion des pesées
//         //   this.display.setCurrent(previousScreen);
//
//	}
//        return abonne;
//    }
    public void abonneDesabonner(int idAbonneInt)
    {
	int index = this.getAbonneIndex(idAbonneInt);

	if (index != -1)
	{
	    this.vecAbonneList.removeElementAt(index);
	    this.delete(index);
	}
    }

    public void abonneDeceder(int idAbonneInt)
    {
	int index = this.getAbonneIndex(idAbonneInt);

	if (index != -1)
	{
	    this.vecAbonneList.removeElementAt(index);
	    this.delete(index);
	}
    }
//
//    public Membre abonneChoisi(int idAbonneInt)
//    {
//	int abonneIndex = this.getAbonneIndex(idAbonneInt);
//        Membre abonne =null;
//	if (abonneIndex != -1)
//	{
//	    IBusinessServices iBusinessServices = BusinessServicesImpl.getInstance();
//	    abonne = iBusinessServices.getAbonneDetails(idAbonneInt);
//	    /// Rediriger directement vers le menu de gestion des pesées
//         //   this.display.setCurrent(previousScreen);
//
//	}
//        return abonne;
//    }

    

  
}

